package com.qianfeng.myymatou.activity;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.myymatou.R;

/**
 * “登录”界面
 */
public class LoginActivity extends AppCompatActivity {

    private Context mContext = this;

    private Toolbar mTbLogin;
    private Button mBtnLogin;
    private TextView mTvIsForgetPassword, mTvRegister;
    private ImageView mIvCancel, mIvLoginAli, mIvLoginWeixin, mIvLoginSina;
    private EditText mEtLoginAccount, mEtLoginPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //找控件
        findView();
        //初始化Toolbar
        initLoginToolbar();
        //初始化登录按钮的状态
        updateLoginButtonState();
        //设置监听
        setClickListener();
    }

    /**
     * 找控件
     */
    private void findView() {
        mTbLogin = (Toolbar) findViewById(R.id.tb_login);
        mEtLoginAccount = (EditText) findViewById(R.id.et_loginAccount);
        mEtLoginPassword = (EditText) findViewById(R.id.et_loginPassword);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mTvIsForgetPassword = (TextView) findViewById(R.id.tv_isForgetPassword);
        mTvIsForgetPassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//设置“忘记密码”的下划线
        mIvLoginAli = (ImageView) findViewById(R.id.iv_login_ali);
        mIvLoginWeixin = (ImageView) findViewById(R.id.iv_login_weixin);
        mIvLoginSina = (ImageView) findViewById(R.id.iv_login_sina);
    }

    /**
     * 初始化Toolbar
     */
    private void initLoginToolbar() {
        View viewInLoginTb = LayoutInflater.from(mContext).inflate(R.layout.layout_tb_login, null);
        mTbLogin.addView(viewInLoginTb);//设置Toolbar中间的布局
        mIvCancel = (ImageView) viewInLoginTb.findViewById(R.id.iv_cancel);
        mTvRegister = (TextView) viewInLoginTb.findViewById(R.id.tv_register);
    }

    /**
     * 根据两个EditText的输入情况，初始化登录按钮的状态
     */
    private void updateLoginButtonState() {
        String loginAccount = mEtLoginAccount.getText().toString();
        String loginPassword = mEtLoginPassword.getText().toString();
        if (!"".equals(loginAccount) && !"".equals(loginPassword)) {
            mBtnLogin.setClickable(true);
            Toast.makeText(mContext, "点击了登录", Toast.LENGTH_SHORT).show();
        } else if (!"".equals(loginAccount) && "".equals(loginPassword)) {
            Toast.makeText(mContext, R.string.please_input_password, Toast.LENGTH_SHORT).show();
        } else if ("".equals(loginAccount) && !"".equals(loginPassword)) {
            Toast.makeText(mContext, R.string.please_input_account, Toast.LENGTH_SHORT).show();
        }else {
            mBtnLogin.setClickable(false);
        }
    }

    /**
     * 设置监听
     */
    private void setClickListener() {
        mIvCancel.setOnClickListener(new LoginOnClickListener());//取消图标
        mTvRegister.setOnClickListener(new LoginOnClickListener());//注册
        mBtnLogin.setOnClickListener(new LoginOnClickListener());//登录按钮
        mTvIsForgetPassword.setOnClickListener(new LoginOnClickListener());//忘记密码
        mIvLoginAli.setOnClickListener(new LoginOnClickListener());//登录支付宝
        mIvLoginWeixin.setOnClickListener(new LoginOnClickListener());//登录微信
        mIvLoginSina.setOnClickListener(new LoginOnClickListener());//登录新浪
    }

    /**
     * 点击事件
     * 其中的Toast是测试用的，字符串暂时不写进strings.xml
     */
    private class LoginOnClickListener implements View.OnClickListener {
        //TODO 点击跳转
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_cancel://取消图标
                    finish();
                    break;
                case R.id.tv_register://注册
                    Toast.makeText(mContext, "点击了注册", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_login://登录按钮
                    updateLoginButtonState();
                    break;
                case R.id.tv_isForgetPassword://忘记密码
                    Toast.makeText(mContext, "啊呀，你忘记密码了啊！我也忘啦！怎破？！", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_login_ali://登录支付宝
                    Toast.makeText(mContext, "登录支付宝", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_login_weixin://登录微信
                    Toast.makeText(mContext, "登录微信", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.iv_login_sina://登录新浪
                    Toast.makeText(mContext, "登录新浪", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

}
